package com.mastercard.assets.web.struts.actions;

import java.io.IOException;
import java.util.Enumeration;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.mastercard.assets.web.struts.forms.FlowProcessForm;
import com.mastercard.assets.web.util.to.UserLogin;

public class FlowProcess
	extends
		Action
{
	public ActionForward execute(
							ActionMapping mapping , 
							ActionForm form ,
							HttpServletRequest request , 
							HttpServletResponse response )
		throws 
			IOException , 
			ServletException
	{
		HttpSession session = request.getSession( ) ;
		
		// en caos de null
		if( session == null )
			return mapping.findForward( "noLogin" ) ;
		
		// toma el userLogin
		UserLogin user = ( UserLogin ) session.getAttribute( "UserLogin" ) ;
		
		// en caso de null
		if( user == null )
			return mapping.findForward( "noLogin" ) ;
		
		FlowProcessForm f = ( FlowProcessForm ) form ;
		
		System.out.println( "$$$$$$$$$$$$$$$$$$$$$$$$$$$$44 " + f ) ;
//		System.out.println( "$$$$$$$$$$$$$$$$$$$$$$$$$$$$44 " + ( f == null ? "" : f.getArchivo01( ) ) ) ;
		
		user.setAttribute( "FlowProcessForma" , f ) ;
		
		int cveWorkPreimplementation = 0 ;
		String cveExit = "" ;
		
		try
		{
			cveWorkPreimplementation = Integer.parseInt( request.getParameter( "cveWorkPreimplementation" ) ) ;
			cveExit = request.getParameter( "exit" ) ;
		}catch( Exception e )
		{
			e.printStackTrace( ) ;
		}
		
		String operation = request.getParameter( "operation" ) ;
                String coments = request.getParameter( "comentarios" ) ;
		
		StringBuffer sb = new StringBuffer( "/FlowProcessIn.do?" ) ;
		
		Enumeration< String > names = request.getParameterNames( ) ;
		
		int index = 0 ;
		
		// recorre
		while( names.hasMoreElements( ) )
		{
			// toma el lemento
			String name = names.nextElement( ) ;
			
			// toma el valor
			String[ ] parameterValues = request.getParameterValues( name ) ;
			
			// en caos de ue no sea null
			if( parameterValues != null )
			{
				int index1 = 0 ;
				
				if( parameterValues != null && parameterValues.length > 0 && index > 0 )
					sb.append( "&" ) ;
				
				for( String valueParam : parameterValues )
				{
					System.out.println( "FlowProcess.java: Request/parameter/" + name + "/" + valueParam ) ;
					
					if( index1 > 0 )
						sb.append( "&" ) ;
					
					sb.append( name ) ;
					sb.append( "=" ) ;
					sb.append( valueParam ) ;
					
					index1++ ;
				}
				
				index++ ;
			}
			else
			{
				String value = request.getParameter( name ) ;
				
				System.out.println( "FlowProcess.java: Request/parameter/" + name + "/" + value ) ;
				
				if( index > 0 )
					sb.append( "&" ) ;
				
				sb.append( name ) ;
				sb.append( "=" ) ;
				sb.append( value ) ;
				
				index++ ;
			}// fin del if-else
		}//fin del while
		
		String res = sb.toString( ) ;
		
		// en caso de que se cumpla, guarda el WorkArea
		System.out.println( "FlowProcess.java: urlResultante/////" + res ) ;
//		user.setNextWorkArea( "/FlowProcessIn.do?cveWorkPreimplementation=" + cveWorkPreimplementation + "&exit=" + ( cveExit == null ? "" : cveExit ) + "&operation=" + ( operation == null ? "" : operation ) ) ;
		user.setNextWorkArea( res ) ;
		
		sb.setLength( 0 ) ;
		sb = null ;
		
	  	return mapping.findForward( "success" ) ;
//	  	return mapping.findForward( "success" ) ;
	}
	
}
